package com.cell.controller;

import com.cell.common.Result;
import com.cell.common.States;
import com.cell.common.exceptions.exceptions.LocationException;
import com.cell.pojo.entity.location.Province;
import com.cell.service.location.ICityService;
import com.cell.service.location.IProvinceService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 位置信息控制器（省，市，区）
 * 国家信息默认China
 *
 * @author cloudJiang
 * @date 2022/6/21 10:41
 */
@RestController
@RequestMapping("/api.location")
@Api(tags = "位置获取相关接口")
public class LocationController {
    @Autowired
    IProvinceService provinceService;
    @Autowired
    ICityService cityService;

    @ApiOperation(value = "获取所有省份", notes = "获取数据库中的所有省份")
    @GetMapping("/getProvince")
    Result getAllProvince(HttpServletRequest request) {
        //获取所有省份
        List<Province> provinces = provinceService.getBaseMapper().selectList(null);
        if (provinces == null) {
            throw new LocationException(States.ERR_SELECT.getStates(), "获取所有省份失败，请稍后再试");
        }
        return new Result(provinces, States.OK.getStates(), States.OK.getDescription());
    }

    @ApiOperation(value = "获取该省份下的所有城市", notes = "参数：省份名字，必填")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "provinceName", value = "要获取城市所属的省份", required = true)
    })
    @GetMapping("/getCity")
    Result getAllCity(
            @ApiParam(name = "provinceName", value = "provinceName", required = true)
            @RequestParam("provinceName") String provinceName) {
        List<Object> city = cityService.getCityByProvinceName(provinceName);
        if (city == null) {
            throw new LocationException(States.ERR_SELECT.getStates(), "获取省份下城市失败，请稍后再试");
        }
        return new Result(city, States.OK.getStates(), States.OK.getDescription());
    }
}
